<?php
/*
boite à outils XML

file version 2005-04-20 10:26:12
*/

// analyse rapide de xml
function searchXmlTags($xml) {
	$name="[a-zA-Z_0-9]+"; $value='[^"]*';
	//$attr=" +($name)=\"($value)\"";
	//$ereg='</?('.$name.')('.$attr.')*/?'.'>'; echo $ereg;
	$ereg='</?('.$name.')[^>]*>';// echo $ereg;
	$n=0; $results=array();
	while (ereg($ereg,$xml,$found)) {
		$tag=&$found[0];
		$attributes=tagAttributes($tag);
		$results[]=array('tag'=>$tag,'name'=>$found[1],'attributes'=>$attributes);
		$xml=ereg_replace($tag,'',$xml);
		$n++; if ($n>50) break;
	}
	return $results;
}

function tagAttributes($tag) {
	$att=array();
	$name="[a-zA-Z_0-9]+"; $value='[^"]*';
	$ereg=" +($name)=\"($value)\"";
	while (ereg($ereg,$tag,$found)) {
		$att[$found[1]]=$found[2];
		$tag=ereg_replace($found[0],'',$tag);
	}
	
	return $att;
}

function & xmlentities($str) {
	$str=str_replace('&','&amp;',$str);
	$str=str_replace('"','&quot;',$str);
	$str=str_replace("'",'&apos;',$str);
	$str=str_replace('<','&lt;',$str);
	$str=str_replace('>','&gt;',$str);
	return $str;
}

// décrit une variable en xml (récursif)
function varToXml($var, $key='',$nivMax=4,$niv=1) {
	$retour="";
	// objets trop complexes ou references menant a une boucle infinie
	if ($niv>$nivMax) return false;
	// determination et affichage du type
	switch($type=gettype($var)) {
		case "boolean":
			if ($var==true) $retour='<boolean key="'.$key.'">true</boolean>'."\n";
			else $retour='<boolean key="'.$key.'">false</boolean>';
			break;
		case "integer":
		case "float":
		case "double":
		case "string":
			$retour.='<'.$type.' key="'.$key.'">'.xmlentities($var).'</'.$type.">\n";
			break;
		case "resource": 
			$retour='<resource key="'.$key.'">'.get_resource_type($var).'</resource>'."\n";
			break;
		case "array": $n=0;
			$retour.='<array key="'.$key.'" size="'.count($var).'">'."\n";
			foreach($var as $varkey => $value) {
				$retour.=varToXml($value,$varkey,$nivMax,$niv);
			}
			$retour.="</array>\n";
			break;
		case "object":
			$retour='<object key="'.$key.'" class="'.get_class($var).'"';
			if ($parent=get_parent_class($var)) $retour.=' extends="'.$parent.'"';
			$retour.=">\n";
			foreach( get_class_methods($var) as $m) 
				$retour.='<method name="'.$m.'" />'."\n";
			if ($members=get_object_vars($var)) {
				$retour.='<members>';
				foreach ($members as $mKey => $mValue)
					$retour.=varToXml($mValue, $mKey,$nivMax,$niv);
				$retour.="</members>\n";
			}
			break;
		case "NULL": 
			$retour.='<null key="'.$key.' />'."\n";
			break;
		default: $retour.='<unkown type="'.$type.'">'."\n";
			break;
	} // fin du switch
	return $retour;
}
/*function varFromXml($xml) {
	$tree=domxml_xmltree($xml);
	echo affvar($tree,'tree');
}
// retourne un tableau 
function varsArrayFromXml($xml) {
	$tree=domxml_xmltree($xml);
	echo affvar($tree);
}
function varsFromDomTree($tree) {

}*/

?>
